package com.kiki.history.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kiki.common.annotation.Log;
import com.kiki.common.core.controller.BaseController;
import com.kiki.common.core.domain.AjaxResult;
import com.kiki.common.enums.BusinessType;
import com.kiki.history.domain.ListeningHistory;
import com.kiki.history.service.IListeningHistoryService;
import com.kiki.common.utils.poi.ExcelUtil;
import com.kiki.common.core.page.TableDataInfo;

/**
 * 收听历史Controller
 * 
 * @author 林志毅
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/history/history")
public class ListeningHistoryController extends BaseController
{
    @Autowired
    private IListeningHistoryService listeningHistoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询收听历史列表
     */
    @PreAuthorize("@ss.hasPermi('history:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(ListeningHistory listeningHistory)
    {
        startPage();
        List<ListeningHistory> list = listeningHistoryService.selectListeningHistoryList(listeningHistory);
        return getDataTable(list);
    }

    /**
     * 导出收听历史列表
     */
    @PreAuthorize("@ss.hasPermi('history:history:export')")
    @Log(title = "收听历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ListeningHistory listeningHistory)
    {
        List<ListeningHistory> list = listeningHistoryService.selectListeningHistoryList(listeningHistory);
        ExcelUtil<ListeningHistory> util = new ExcelUtil<ListeningHistory>(ListeningHistory.class);
        util.exportExcel(response, list, "收听历史数据");
    }

    /**
     * 获取收听历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('history:history:query')")
    @GetMapping(value = "/{historyId}")
    public AjaxResult getInfo(@PathVariable("historyId") Long historyId)
    {
        return success(listeningHistoryService.selectListeningHistoryByHistoryId(historyId));
    }

    /**
     * 新增收听历史（存入redis）
     */
    @PreAuthorize("@ss.hasPermi('history:history:add')")
    @Log(title = "收听历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ListeningHistory listeningHistory) {
        String userPrefix = "user:"; // 必须要有，不然没有目录效果
        String userPrefix2 = listeningHistory.getUserId() + ":";
        String userPrefix3 = "listeningHistory";
        String redisKey = userPrefix + userPrefix2+ userPrefix3; // 使用用户ID作为队列的键

        // 将 JSON 字符串添加到 Redis 列表中
        redisTemplate.opsForList().rightPush(redisKey, listeningHistory.getSongId());

//        // 修剪列表，使其只包含最后 10 条记录
//        redisTemplate.opsForList().trim(redisKey, -10, -1);
//        // 设置键的过期时间，例如 7 天
//        redisTemplate.expire(redisKey, 15, TimeUnit.SECONDS);
        // 插入数据库
        int rows = listeningHistoryService.insertListeningHistory(listeningHistory);
        return toAjax(rows);
    }



    /**
     * 修改收听历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:edit')")
    @Log(title = "收听历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ListeningHistory listeningHistory)
    {
        return toAjax(listeningHistoryService.updateListeningHistory(listeningHistory));
    }

    /**
     * 删除收听历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:remove')")
    @Log(title = "收听历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds)
    {
        return toAjax(listeningHistoryService.deleteListeningHistoryByHistoryIds(historyIds));
    }
}
